package dataBase;

import java.sql.SQLException;
import java.util.List;

public class CheckLoginWithDBData {
	public static boolean checkIfIndexesOnBothListsAreTheSame(String username,
			String password) throws ClassNotFoundException, SQLException {

		List<String> usernames = GetColumnsAsListsFromDB
				.getExistingUserNamesAsList(DataBase.getDbCon());
		List<String> passwords = GetColumnsAsListsFromDB
				.getExistingPasswordsAsList(DataBase.getDbCon());

		String[] usernamesArray = usernames
				.toArray(new String[usernames.size()]);
		String[] passwordsArray = passwords
				.toArray(new String[passwords.size()]);

		int index = getIndex(username, usernamesArray);
		int index2 = getIndex(password, passwordsArray);
		if (index == index2) {
			return true;
		} else
			return false;
	}

	private static int getIndex(String username, String[] usernamesArray) {
		int index = 0;
		for (int i = 0; i < usernamesArray.length; i++) {
			if (usernamesArray[i].contains(username)) {
				index = i;
			}
		}
		return index;
	}

	public static boolean checkIfPasswordExistsInDB(String password)
			throws SQLException, ClassNotFoundException {
		return GetColumnsAsListsFromDB.getExistingPasswordsAsList(
				DataBase.getDbCon()).contains(password);
	}

	public static boolean checkIfUsernameExistsInDB(String username)
			throws SQLException, ClassNotFoundException {
		return GetColumnsAsListsFromDB.getExistingUserNamesAsList(
				DataBase.getDbCon()).contains(username);
	}
}
